using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using THSLib;using THSERP.BaseProvider;
using DevExpress.XtraGrid.Views.Grid;
using System.Data;using THSERP.BusinessEntities;
using System.Windows.Forms;

namespace THSERP.Modules.CompanyConstant
{
    public partial class HRShiftsGridControl : THSGridControl
    {
        public override void InitGridControlDataSource()
        {
            base.InitGridControlDataSource();
            CompanyConstantEntities entity = (CompanyConstantEntities)((BaseModuleERP)Screen.Module).CurrentModuleEntity;
            DataSet ds = new HRShiftsController().GetAllObjects();
            BindingSource bds = new BindingSource();
            entity.HRShiftsList.Invalidate(ds);
            bds.DataSource = entity.HRShiftsList;
           this.DataSource = bds;
            this.RefreshDataSource();
        }

        protected override GridView InitializeGridView()
        {

            DevExpress.XtraGrid.Views.Grid.GridView gridView = base.InitializeGridView();
            foreach (DevExpress.XtraGrid.Columns.GridColumn column in gridView.Columns)
               column.OptionsColumn.AllowEdit = true;
            gridView.OptionsView.ShowNewItemRow = true;
            gridView.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;
            gridView.KeyUp += new System.Windows.Forms.KeyEventHandler(GridView_KeyUp);
            return gridView;
        }

        protected override void GridView_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            base.GridView_ValidateRow(sender, e);

            DevExpress.XtraGrid.Views.Grid.GridView gridView = (DevExpress.XtraGrid.Views.Grid.GridView)this.MainView;
            //try
            //{
            //   gridView.Columns["HRShiftNo"]

            //}
            //catch (Exception)
            //{
            //    gridView.FocusedRowHandle = e.RowHandle;
            //    if (gridView.Columns["GENumberingLength"] != null)
            //        gridView.FocusedColumn = gridView.Columns["GENumberingLength"];
            //}
        }
        protected override void GridView_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            base.GridView_KeyUp(sender, e);
            GridView gridView = (GridView)sender;
            if (e.KeyCode == Keys.Delete)
            {
                
                DataView dataView = (DataView)gridView.DataSource;
                DataRow row = (DataRow)gridView.GetDataRow(gridView.FocusedRowHandle);
                if (row != null)
                {
                    HRShiftsInfo objHRShiftsInfo = (HRShiftsInfo)new HRShiftsController().GetObjectFromDataRow(row);
                    if (objHRShiftsInfo != null)
                    {
                        new HRShiftsController().DeleteObject(objHRShiftsInfo.HRShiftID);
                        dataView.Table.Rows.Remove(row);
                    }
                }
            }
            gridView.RefreshData();

        }
    }
}
